/**
 * @author sanyu0302
 * @qq  757178971
 * @description:
 */

package com.wehealth.widget;
import com.wehealth.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class GridChart extends View{
    
    private static final String TAG="GridChart";

    /*以下是默认值*/
    
    /** 默认背景色 */
    /** 默认经线数 */
    public static final int DEFAULT_LOGITUDE_NUM = 7;
    
    /** 默认纬线数 */
    public static final int DEFAULT_LATITUDE_NUM = 21;
    
    /**默认底部文字区域的宽度*/
    public static final int DEFAULT_BOTTOM_TEXT_SPACE_WIDTH = 50;
    
    public static final int DEFAULT_LEFT_TEXT_SPACE_WIDTH = 3;

    
    public float default_left_text_space_width;
    public float default_left_right_margin_width;
    public float default_bottom_text_space_height;
    /**最上面的边框高度*/
   public float default_margin_top;
    
    public float uper_chart_bottom;
    public float longitudeSpacing;
    public float latitudeSpacing;
    public float chart_height;
    
    private PathEffect mPaintPathEffect;
    
    private String[] resultValue;
    private int valuesum;
    private String[] valuesStr;


    public GridChart(Context context) {
        this(context, null);
        // TODO Auto-generated constructor stub
    }
    
    public GridChart(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        // TODO Auto-generated constructor stub
    }
    
    public GridChart(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub
        initView();
    }
    
    
    private void initView(){
        
        default_left_text_space_width = getResources().getDimension(R.dimen.grid_chart_defualt_left_space_size);
        default_bottom_text_space_height =
                getResources().getDimension(R.dimen.grid_chart_defualt_bottom_space_size);
        default_left_right_margin_width = 
                getResources().getDimension(R.dimen.grid_chart_left_right_space_size);
        default_margin_top = 
                getResources().getDimension(R.dimen.grid_chart_margin_top);
        mPaintPathEffect = new DashPathEffect(new float[] { 10, 2, 10, 2 }, 1);
    }
    
    
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        setBackgroundResource(R.drawable.bg_white_cirecle);
        /** 获取整个View的宽高 */
        int viewHeight = getHeight();
        int viewWidth = getWidth();
        
        uper_chart_bottom = viewHeight - default_bottom_text_space_height;
        longitudeSpacing = (viewWidth - default_left_text_space_width-
                2*default_left_right_margin_width)/(DEFAULT_LOGITUDE_NUM-1);
        
        chart_height = viewHeight - default_margin_top - default_bottom_text_space_height;
        latitudeSpacing =chart_height/(DEFAULT_LATITUDE_NUM-1);
        
        
        
        //绘制经线
        drawLongitudes(canvas,viewHeight,longitudeSpacing);
        
        //绘制星期
        drawTextWeek(canvas,viewHeight,longitudeSpacing);
        
        //画纬线
        drawLatitudes(canvas, viewHeight, viewWidth, latitudeSpacing);
         
        drawResultLine(canvas);
        
        drawLineValue(canvas,viewHeight,latitudeSpacing);
    }
    
   
    /**
     * 绘制经线
     * 
     * @param canvas
     * @param viewHeight
     * @param viewWidth
     */
    private void drawLongitudes(Canvas canvas, int viewHeight, float longitudeSpacing) {
        Paint paint = new Paint();
        paint.setPathEffect(mPaintPathEffect);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(1);
        paint.setColor(getResources().getColor(R.color.text_color_grey));
        Path path = new Path();
       for (int i = 0; i < DEFAULT_LOGITUDE_NUM; i++) {
            path.moveTo(default_left_text_space_width+default_left_right_margin_width + 
                    longitudeSpacing * i, default_margin_top+1);
            path.lineTo(default_left_text_space_width+default_left_right_margin_width + 
                    longitudeSpacing * i,uper_chart_bottom);
            canvas.drawPath(path, paint);
        }

    }
    
    
    /**
     * 绘制纬线
     * 
     * @param canvas
     * @param viewHeight
     * @param viewWidth
     */
    private void drawLatitudes(Canvas canvas, int viewHeight, int viewWidth, float latitudeSpacing) {
        Paint paint = new Paint();
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(1);
        paint.setPathEffect(mPaintPathEffect);
  
        Path path = new Path();
        canvas.drawLine(default_left_text_space_width, default_margin_top+1, viewWidth, default_margin_top+1, paint);
        for (int i = 1; i < DEFAULT_LATITUDE_NUM; i++) {
            int result = i%5;
            if(result==0){
                canvas.drawLine(default_left_text_space_width, 
                        default_margin_top+1 + latitudeSpacing * i, 
                        viewWidth, 
                        default_margin_top+1 + latitudeSpacing * i, paint);
                
            }else{
                paint.setColor(getResources().getColor(R.color.text_color_grey));
                path.moveTo(default_left_text_space_width, default_margin_top+1 + latitudeSpacing * i);
                path.lineTo(viewWidth,default_margin_top+1 + latitudeSpacing * i);
                canvas.drawPath(path, paint);
                }

        }
    }
    
    
    private void drawTextWeek(Canvas canvas, int viewHeight, float longitudeSpacing){
        Paint textPaint = new Paint();
        float textSize = getResources().getDimension(R.dimen.grid_chart_week_text_size);
        textPaint.setTextSize(textSize);
         String[] weeks = getResources().getStringArray(R.array.grid_chart_weeks);
        for (int i = 0; i < DEFAULT_LOGITUDE_NUM; i++) {
            canvas.drawText(weeks[i],
                    default_left_text_space_width+default_left_right_margin_width + 
                    longitudeSpacing * i-DEFAULT_BOTTOM_TEXT_SPACE_WIDTH/2,
                    viewHeight - default_bottom_text_space_height/2 + textSize/2,
                    textPaint);
        }

    }
    
    
    public void setResultValues(String[] vaules,int sum,String[] valuesAll){
        resultValue = vaules;
        valuesum = sum;
        this.valuesStr = valuesAll;
        invalidate();
      }
    
    private void drawResultLine(Canvas canvas){
        if(resultValue!=null && resultValue.length>0){
            Paint paint = new Paint();
            paint.setStyle(Style.STROKE);
            paint.setStrokeWidth(3);
            paint.setColor(getResources().getColor(R.color.red));
            
            Paint paintCircle = new Paint();
            paintCircle.setStyle(Style.FILL);
            paintCircle.setColor(getResources().getColor(R.color.red));
            
            float[] x = new float[7];
            float[] y = new float[7];
            
            for(int i = 0; i < 7;i++){
                x[i] = default_left_text_space_width+default_left_right_margin_width + 
                        longitudeSpacing * i;
               }
            for(int j =0 ; j<7;j++){   
                float realvalues = Float.valueOf(resultValue[j]);  
                float times = valuesum/20;     
                float values =realvalues/times;
                y[j] = default_margin_top+1+latitudeSpacing*(20-(int)values)+
                		latitudeSpacing*(values - (int)values);
             }

          for(int n=0;n<6;n++){  
            canvas.drawLine(x[n], y[n],x[n+1], y[n+1], paint);
            canvas.drawCircle(x[n], y[n], 10, paintCircle);
            }
          canvas.drawCircle(x[6], y[6], 10, paintCircle);
        }
    }
    
    private void drawLineValue(Canvas canvas, int viewHeight, float latitudeSpacing){
        if(valuesStr!=null && valuesStr.length>0){
            Paint textPaint = new Paint();
            float textSize = getResources().getDimension(R.dimen.grid_chart_week_text_size);
            textPaint.setTextSize(textSize);
            //String[] values = getResources().getStringArray(R.array.grid_chart_sugar_value);
              int count = valuesStr.length;
              int index = 0;
            for (int i = 0; i < DEFAULT_LATITUDE_NUM; i++) {
                int result = i%5;
                if(result==0 && index < count){
                    canvas.drawText(valuesStr[index],
                      0,
                      default_margin_top + latitudeSpacing * i+textSize/2,
                      textPaint);
                    index++;
                }
            }
        }

    }
}
